<template>
  <div>
    <table class="layui-table">
      <colgroup>
        <col width="150" />
        <col width="200" />
        <col />
      </colgroup>
      <thead class="textcenter2">
        <tr>
          <th>帖子标题</th>
          <th>状态</th>
          <th>结帖</th>
          <th>发表时间</th>
          <th>数据</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr
          class="textcenter"
          v-for="(item, index) in page"
          :key="'page' + index"
        >
          <td>
            <router-link
              :class="{ detail: item.title }"
              :to="{ name: 'detail', params: { tid: item._id } }"
            >
              {{ item.title }}
            </router-link>
          </td>
          <td>{{ item.status === '0' ? '打开' : '关闭' }}</td>
          <td :class="{ end: item.isEnd !== '0' }">
            {{ item.isEnd === '0' ? '未结' : '已结帖' }}
          </td>
          <td>{{ item.created | moment }}</td>
          <td>{{ item.reads }}阅/{{ item.answer }}答</td>
          <td>
            <button
              class="layui-btn layui-btn-xs"
              :class="{ 'layui-btn-disabled': item.isEnd === '1' }"
              @click="check(item)"
            >
              编辑
            </button>
            <button
              class="layui-btn layui-btn-xs layui-btn-danger"
              @click="delet(item)"
            >
              删除
            </button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import { postList, setdel } from '@/api/user'
export default {
  name: "accounts",
  components: {},
  props: {

  },
  data () {
    return {
      page: ''
    };
  },
  computed: {},
  created () { },
  mounted () {
    this.getPost();
  },
  watch: {},
  methods: {
    check (item) {
      if (item.isEnd === '1') {
        this.$pop('shake', '帖子已经结贴无法编辑')
        return
      }
      else {
        this.$router.push({
          name: 'edit',
          params: {
            tid: item._id, page: item
          }
        })
      }
    },
    getPost () {
      postList({
        uid: this.$store.state.userInfo._id
      }).then((res) => {
        if (res.code === 200) {
          this.page = res.data
        }
      })
    },
    delet (item) {
      this.$confirm('你确定要删除该文章吗？', () => {
        if (item.isEnd === '1') {
          this.$pop('shake', '帖子已经结帖无法删除')
          return
        }
        setdel(
          item._id
        ).then((res) => {
          if (res.code === 200) {
            this.$pop('', '删除文章成功')
            //刷新列表
            this.page.splice(this.page.indexOf(item), 1)
          }
        })
      }, () => { })
    }
  },
};
</script>

<style scoped lang="scss">
.detail {
  color: #01aaed;
}
.end {
  color: #ff5722;
}
</style>
